/*
 * @Author: sdq 483657864@qq.com
 * @Date: 2025-04-17 17:31:33
 * @LastEditors: sdq 483657864@qq.com
 * @LastEditTime: 2025-04-18 10:36:51
 * @FilePath: \ranyu_anchor_new\src\store\modules\agroa\modules\remoteUser.ts
 * @Description: 远端用户（上麦）用户状态
 */
import { type UID } from "agora-rtc-sdk-ng";
import { defineStore } from "pinia";
interface RemoteUser {
  uid: UID;
  subscribed: boolean;
}
type UpdateType = "add" | "del";
interface RemoteUserStatus {
  activeSpeakers: UID[]; //活跃用户
  remoteUsers: RemoteUser[]; // 所有远端用户（无论订阅状态）
  manualUnsubscribed: Set<UID>; // 手动取消订阅的用户（黑名单）
}
export const useRemoteStore = defineStore("remoteuser", {
  state: (): RemoteUserStatus => ({
    activeSpeakers: [],
    remoteUsers: [],
    manualUnsubscribed: new Set(),
  }),
  actions: {
    /**
     *更新活跃用户列表
     * @param {UID[]}actionUsers  - 活跃用户
     */
    updateActiveSpeakers(actionUsers: UID[]) {
      this.activeSpeakers = actionUsers;
    },
    /**
     *更新远端用户列表（已推流上麦用户）
     * @param {UpdateType}updateType - 更新的类型
     * @param {RemoteUser}remoteUsersUid - 需要操作的对象的UID
     */
    updateRemoteUsers(updateType: UpdateType, remoteUsersUid: UID) {
      if (updateType === "add") {
        if (!this.remoteUsers.some((u) => u.uid === remoteUsersUid)) return;
        this.remoteUsers.push({
          uid: remoteUsersUid,
          subscribed: !this.manualUnsubscribed.has(remoteUsersUid),
        });
      } else {
        this.remoteUsers = this.remoteUsers.filter((u) => u.uid !== remoteUsersUid);
      }
    },
    /**
     * 更新黑名单
     * @param {UpdateType}updateType - 更新的类型
     * @param {UID}uid - 目标用户ID
     */
    updateManualUnsubscribed(updateType: UpdateType, uid: UID) {
      if (updateType === "add") {
        this.manualUnsubscribed.add(uid);
      } else {
        this.manualUnsubscribed.delete(uid);
      }
      // this.manualUnsubscribed.delete(remoteUsers.uid);
    },
    /**
     * 更新用户订阅状态(所有上麦用户)
     * @param {UID}uid - 用户ID
     * @param {boolean}status - 新的订阅状态
     */
    updateUserSubscription(uid: UID, status: boolean) {
      const index = this.remoteUsers.findIndex((u) => u.uid === uid);
      if (index !== -1) {
        this.remoteUsers[index].subscribed = status;
      }
    },
  },
});
